﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Function;

namespace UI.UserControl.UI
{
    public partial class Query : System.Web.UI.UserControl
    {
        public delegate void BindData(string where = "", bool issearch = false);

        public BindData bindData;

        public bool QuestQuery
        {
            set
            {
                ViewState["q_qq"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if ((bool)(ViewState["q_qq"] ?? false))
                //this.Page.Form.DefaultButton = queryBtn.ID;
            }
        }

        public void BindQueryType(string[] typenames, string[] typeFileds, Hashtable filedTypes = null, Hashtable datasource = null, Hashtable tip = null)
        {
            Session["query_ft"] = filedTypes;
            Session["query_ds"] = datasource;
            Session["query_tip"] = tip;
            queryBtn.Visible = true;
            queryType.Visible = true;
            List<ListItem> datas = new List<ListItem>();
            ListItem li;
            for (int i = 0; i < typenames.Length; i++)
            {
                li = new ListItem();
                li.Text = typenames[i];
                li.Value = typeFileds[i];
                datas.Add(li);
            }
            queryType.DataTextField = "Text";
            queryType.DataValueField = "Value";
            queryType.DataSource = datas;
            queryType.DataBind();
            queryType.SelectedIndex = 0;
            queryType_SelectedIndexChanged(null, null);
        }

        protected void queryBtn_Click(object sender, EventArgs e)
        {
            if (queryType1.Visible && queryType2.Visible && queryType3.Visible && queryType4.Visible && queryType5.Visible)
            {
                MessageBox mb = new MessageBox();
                mb.ErrorBox(this.Page, "最多支持5个条件");
                return;
            }

            if (queryText.Visible && queryText.Text.Trim() == string.Empty && queryLastTime.Text.Trim() == string.Empty)
            {
                MessageBox mb = new MessageBox();
                mb.ErrorBox(this.Page, "请先输入查询条件");
                return;
            }

            if (queryLastTime.Visible && queryText.Text.IsNotEmpty() && queryLastTime.Text.IsNotEmpty() && queryText.Text.ToDate() > queryLastTime.Text.ToDate())
            {
                MessageBox mb = new MessageBox();
                mb.ErrorBox(this.Page, "开始时间不能大于结束时间");
                return;
            }

            Session["newQurey"] = "1";

            Hashtable wheres = (Hashtable)(ViewState["query_where"] ?? (ViewState["query_where"] = new Hashtable()));
            bool isHave = false;

            foreach (string key in wheres.Keys)
            {
                string[] where = wheres[key] as string[];
                if (where[0] == queryType.SelectedValue)
                {
                    where[1] = getInputValue();
                    ((Label)queryTaskCells.FindControl(key)).Text = queryType.SelectedItem.Text + "[" + getInputText() + "]";
                    isHave = true;
                    break;
                }
            }
            if (!isHave)
            {
                int index = 1;
                for (int j = 1; j < 6; j++)
                {
                    if (!queryTaskCells.FindControl("queryType" + j).Visible)
                    {
                        index = j;
                        break;
                    }
                }

                Label lbQueryType = queryTaskCells.FindControl("queryType" + index) as Label;
                ImageButton ibCancel = queryTaskCells.FindControl("cancel" + index) as ImageButton;
                lbQueryType.Visible = true;
                ibCancel.Visible = true;
                lbQueryType.Text = queryType.SelectedItem.Text + "[" + getInputText() + "]";

                string[] newWhere = new string[] { queryType.SelectedItem.Value, getInputValue() };
                wheres.Add(ibCancel.CommandName, newWhere);

            }
            CreateWhereStr();
        }

        private string getInputText()
        {
            string text = string.Empty;
            if (querySelect.Visible)
            {
                text = querySelect.SelectedItem.Text;
            }
            else if (!queryTo.Visible)
            {
                text = queryText.Text.Trim();
            }
            else
            {
                if (queryText.Text.Trim() == string.Empty)
                {
                    text = "小于 " + queryLastTime.Text.Trim();
                }
                else if (queryLastTime.Text.Trim() == string.Empty)
                {
                    text = "大于 " + queryText.Text.Trim();
                }
                else
                {
                    text = queryText.Text.Trim() + "至" + queryLastTime.Text.Trim();
                }
            }
            return text;
        }

        private string getInputValue()
        {
            string value = string.Empty;
            if (querySelect.Visible)
            {
                value = querySelect.SelectedItem.Value;
            }
            else if (!queryTo.Visible)
            {
                value = queryText.Text.Trim();
            }
            else
            {
                value = queryText.Text.Trim() + "※" + queryLastTime.Text.Trim();
            }
            return value;
        }

        protected void CreateWhereStr()
        {
            Hashtable wheres = (Hashtable)(ViewState["query_where"] ?? (ViewState["query_where"] = new Hashtable()));
            Hashtable ht = (Hashtable)Session["query_ft"];
            string whereStr = string.Empty;
            foreach (string[] where in wheres.Values)
            {
                if (ht[where[0]] == "t")
                {
                    string[] temp = where[1].Split('※');
                    if (temp[0] != string.Empty)
                    {
                        whereStr += where[0] + " >= '" + temp[0] + " 00:00:00" + "' and ";
                    }
                    if (temp[1] != string.Empty)
                    {
                        whereStr += where[0] + " <= '" + temp[1] + " 23:59:59" + "' and ";
                    }

                }
                else if (ht[where[0]] == "d")
                {
                    whereStr += where[0] + " = '" + where[1] + "' and ";
                }
                else
                {
                    whereStr += where[0] + " like '%" + where[1] + "%' and ";
                }
            }
            if (whereStr != string.Empty)
                whereStr = whereStr.Substring(0, whereStr.Length - 4);
            Session["condition"] = whereStr;
            if (bindData != null)
            {
                bindData(whereStr, true);
            }
        }

        protected void cancelQuery_Click(object sender, ImageClickEventArgs e)
        {

            ImageButton ib = ((ImageButton)sender);
            ib.Visible = false;
            ib.Parent.FindControl(ib.CommandName).Visible = false; ;
            Hashtable wheres = (Hashtable)(ViewState["query_where"] ?? (ViewState["query_where"] = new Hashtable()));
            wheres.Remove(ib.CommandName);
            CreateWhereStr();
        }

        protected void queryType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Hashtable ht = (Hashtable)Session["query_ft"];
            if (ht != null)
            {
                string mode = (string)ht[queryType.SelectedValue];
                if (mode == null)
                {
                    if (queryLastTime.Visible)
                    {
                        queryLastTime.Text = string.Empty;
                        queryText.Text = string.Empty;
                    }
                    queryText.Visible = true;
                    querySelect.Visible = false;
                    queryTo.Visible = false;
                    queryLastTime.Visible = false;
                    queryText.Attributes["onclick"] = "";
                    SetInputTip();
                }
                else if (mode == "t")
                {
                    if (!queryLastTime.Visible)
                    {
                        queryText.Text = string.Empty;
                    }
                    queryText.Visible = true;
                    querySelect.Visible = false;
                    queryTo.Visible = true;
                    queryLastTime.Visible = true;
                    queryText.Attributes["onclick"] = "WdatePicker({dateFmt:'yyyy-MM-dd'})";
                }
                else if (mode == "d")
                {
                    queryText.Text = string.Empty;
                    queryLastTime.Text = string.Empty;
                    querySelect.Visible = true;
                    queryText.Visible = false;
                    queryTo.Visible = false;
                    queryLastTime.Visible = false;
                    ht = (Hashtable)Session["query_ds"];
                    querySelect.DataTextField = "Text";
                    querySelect.DataValueField = "value";
                    querySelect.DataSource = ht[queryType.SelectedValue];
                    querySelect.DataBind();
                }

            }
        }

        private void SetInputTip()
        {
            if (Session["query_tip"] != null)
            {
                Hashtable tip = (Hashtable)Session["query_tip"];
                string tipInfo = (string)tip[queryType.SelectedValue];
                if (tipInfo != null)
                {
                    //string[] tipInfos = tipInfo.Split(',');
                    //string filed = tipInfos[0];
                    //string tname = tipInfos[1];
                    //string where = tipInfos[2];
                    queryText.Attributes["onkeypress"] = "showTip('" + queryTipBtn.ClientID + "');";
                }
            }
        }

        protected void queryTipBtn_Click(object sender, EventArgs e)
        {

        }
    }
}